﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;
using CE.Database;

public partial class management_ManagementHome : System.Web.UI.Page
{
    string PageCode = "CXTHRHOM";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("ManagementSecureLogin.aspx");
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
        if (Session[CE.Global.General.UserRole] != null && Session[CE.Global.General.UserRole].ToString() == "200")
        {
            pnlMenu.Visible = true;
            divSalesGroup.Visible = false;
        }
        else
        {
            pnlMenu.Visible = false;
            divSalesGroup.Visible = true;
        }
        if (!IsPostBack)
        {
            CE.User.AccountStatusDB _accountStatusdb = new CE.User.AccountStatusDB();
            int _pwdStatus = _accountStatusdb.GetLastPasswordChagedStatus(Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));
            if (_pwdStatus == 1)
            {
                Response.Redirect("~/user/account/ChangePassword.aspx");
            }

            GetSalesGropChallenge();
        }
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("UserMessage.aspx?menu=0&mt=UM&ec=100");
        }
    }

    private void GetSalesGropChallenge()
    {
        DateTime dt = CE.Global.General.GetUTCDateTime();
        //lblSalesGroupTitle.Text = "New Sales Group Challenge " + dt.ToString("MMMM") + " " + dt.ToString("yyyy");
        lblSalesGroupTitle.Text = dt.ToString("MMMM") + " " + dt.ToString("yyyy");
        decimal _decAllAmount = 0;
        decimal _decAllAmountRenewal = 0;
        decimal _decAllAmountTotal = 0;
        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        string _spName = "sprocCXCRMNewSalesGroupChallengeSelectedItemList";
        DataSet _dsAmount = new DataSet();
        _dsAmount = _db.GetDataSet(_spName);
        if (_dsAmount != null && _dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
        {
            if (!decimal.TryParse(_dsAmount.Tables[0].Rows[0]["AllAmount"].ToString(), out _decAllAmount))
            {
                _decAllAmount = 0;
            }
            if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
            {
                grvSalesGroupChallenge.DataSource = _dsAmount.Tables[1];
                grvSalesGroupChallenge.DataBind();
            }
            else
            {
                grvSalesGroupChallenge.DataSource = null;
                grvSalesGroupChallenge.DataBind();
            }
        }
        if (_dsAmount != null && _dsAmount.Tables.Count > 2 && _dsAmount.Tables[2].Rows.Count > 0)
        {
            if (!decimal.TryParse(_dsAmount.Tables[2].Rows[0]["AllAmount"].ToString(), out _decAllAmountRenewal))
            {
                _decAllAmountRenewal = 0;
            }
            if (_dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
            {
                grvSalesGroupChallengeRenewal.DataSource = _dsAmount.Tables[3];
                grvSalesGroupChallengeRenewal.DataBind();
            }
            else
            {
                grvSalesGroupChallengeRenewal.DataSource = null;
                grvSalesGroupChallengeRenewal.DataBind();
            }
        }
        if (_decAllAmount > 0)
        {
            lblChallengeAmount.Text = _decAllAmount.ToString("#0,000.00");
        }
        if (_decAllAmountRenewal > 0)
        {
            lblChallengeAmountRenewal.Text = _decAllAmountRenewal.ToString("#0,000.00");
        }
        _decAllAmountTotal = _decAllAmount + _decAllAmountRenewal;
        lblSalesGroupTitle.Text += " - Total GG Collection " + _decAllAmountTotal.ToString("#0,000.00");
    }

}
